package com.wssnail.xxl.job.config;

import cn.hutool.core.util.StrUtil;
import com.wssnail.xxl.job.properties.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: 自动装配的类
 * @author 熟透的蜗牛
 * @date 2023/1/8 12:48
 * @version 1.0
 */
@Configuration
@EnableConfigurationProperties(XxlJobProperties.class)
@Slf4j
public class XxlJobAutoConfig {

    @Autowired
    private XxlJobProperties xxlJobProperties;

    @Bean("xxlJobExecutor")
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>>>>>>>>>>>>>init");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(StrUtil.isBlank(xxlJobProperties.getAdminAddresses()) ? "http://127.0.0.1:8080/xxl-job-admin" : xxlJobProperties.getAdminAddresses());
        xxlJobSpringExecutor.setAppname(StrUtil.isBlank(xxlJobProperties.getAppName()) ? "demo" : xxlJobProperties.getAppName());
        xxlJobSpringExecutor.setAddress(xxlJobProperties.getAddress());
        xxlJobSpringExecutor.setIp(xxlJobProperties.getIp());
        xxlJobSpringExecutor.setPort(xxlJobProperties.getPort() == 0 ? 9999 : xxlJobProperties.getPort());
        xxlJobSpringExecutor.setAccessToken(StrUtil.isBlank(xxlJobProperties.getAccessToken()) ? "default_token" : xxlJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(StrUtil.isBlank(xxlJobProperties.getLogPath()) ? "/data/applogs/xxl-job/jobhandler" : xxlJobProperties.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays() == 0 ? 30 : xxlJobProperties.getLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}
